package com.dehui.traders.ui.activity;

import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.dehui.traders.BR;
import com.dehui.traders.R;
import com.dehui.traders.bean.F10Bean;
import com.dehui.traders.bean.FinancialBean;
import com.dehui.traders.bean.NewBean;
import com.dehui.traders.bean.ShareHolderBean;
import com.dehui.traders.databinding.ActivityStocksDetailBinding;
import com.dehui.traders.kchart.MyCombinedChart;
import com.dehui.traders.ui.viewmodle.StocksDetailViewModel;
import com.dehui.traders.utils.SimulatedDataManager;
import com.dehui.traders.widgets.BaseViewHolder;
import com.dehui.traders.widgets.CommonAdapter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.Utils;

/**
 * Created by hqq on 18.4.2.
 */

public class StocksDetailActivity extends BaseActivity<ActivityStocksDetailBinding, StocksDetailViewModel> {

    public static final String IntentKey_StocksName = "IntentKey_StocksName";
    private String mStockName;
    private List<NewBean> newsList = new ArrayList<>();
    private List<NewBean> announcementList = new ArrayList<>();
    private CommonAdapter<NewBean> mNewAdapter;
    private CommonAdapter<NewBean> mAnnouncementAdapter;
    private F10Bean mF10Bean;
    private ShareHolderBean mShareHolderBean;
    private FinancialBean mFinancialBean;
    private MyCombinedChart mChartKline;
    private StocksDetailViewModel model;

    @Override
    public int initContentView() {
        return R.layout.activity_stocks_detail;
    }

    @Override
    public void initParam() {
        Bundle bundle = getIntent().getBundleExtra("bundle");
        mStockName = bundle.getString(IntentKey_StocksName);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            landscapeChange();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            portrait();
        }
    }

    private void portrait() {
        binding.tablayoutBottomStocksDetail.setVisibility(View.VISIBLE);
        binding.flContainer.setVisibility(View.VISIBLE);
        binding.toolbar.setVisibility(View.VISIBLE);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) binding.mycombinedchartKLine.getLayoutParams();
        lp.height = Utils.dp2px(this,300);
        binding.mycombinedchartKLine.setLayoutParams(lp);
    }

    private void landscapeChange() {
        binding.tablayoutBottomStocksDetail.setVisibility(View.GONE);
        binding.flContainer.setVisibility(View.GONE);
        binding.toolbar.setVisibility(View.GONE);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) binding.mycombinedchartKLine.getLayoutParams();
        lp.height = Utils.dp2px(this,Utils.px2dp(this,Utils.getScreenWidth(this)) - 36);
        binding.mycombinedchartKLine.setLayoutParams(lp);
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public StocksDetailViewModel initViewModel() {
        model = new StocksDetailViewModel(this, mStockName);
        return model;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
        initUI();
        initDetail();
    }

    private void initDetail() {
        initShareHolderDetail();
        initF10Detail();
        initFinancialDetail();
        model.initKLine(mChartKline);
    }

    private void initFinancialDetail() {

        binding.includeFinancial.tvBasePerStocksEarnings.setText(mFinancialBean.basePerStocksEarnings);
        binding.includeFinancial.tvPerStocksNetAsset.setText(mFinancialBean.perStocksNetAsset);
        binding.includeFinancial.tvPerStocksAccumulationFund.setText(mFinancialBean.perStocksAccumulationFund);
        binding.includeFinancial.tvPerStocksUndistributedProfits.setText(mFinancialBean.perStocksUndistributedProfits);
        binding.includeFinancial.tvPerStocksCashFlow.setText(mFinancialBean.perStocksCashFlow);
        binding.includeFinancial.tvNetAssetsIncomeRate.setText(mFinancialBean.netAssetsIncomeRate);
        binding.includeFinancial.tvFinancialTotalEquity.setText(mFinancialBean.totalEquity);
        binding.includeFinancial.tvPriceEarningRatioTtm.setText(mFinancialBean.priceEarningRatioTTM);
        binding.includeFinancial.tvPriceEarningRatio.setText(mFinancialBean.priceEarningRatio);
        binding.includeFinancial.tvPriceValueRatio.setText(mFinancialBean.priceValueRatio);

        List<Integer> colorList = new ArrayList<>();
        colorList.add(R.color.red);
        colorList.add(R.color.green);
        //  添加的是百分比
        List<Float> rateList = new ArrayList<>();
        rateList.add(70f);
        rateList.add(30f);
        binding.includeFinancial.ringviewFinancial.setShow(colorList, rateList,true,true);
    }

    private void initShareHolderDetail() {
        binding.includeShareholders.tvTotalEquity.setText(mShareHolderBean.totalEquity);
        binding.includeShareholders.tvAllAStocks.setText(mShareHolderBean.allAStocks);
        binding.includeShareholders.tvCirculateAStocks.setText(mShareHolderBean.circulateAStocks);
        binding.includeShareholders.tvEffectiveCirculateAStocks.setText(mShareHolderBean.effectiveCirculateAStocks);
        binding.includeShareholders.tvCirculateAStocks.setText(mShareHolderBean.circulateAStocks);
        binding.includeShareholders.tvFundNumMsg0.setText(mShareHolderBean.fundNums[0]);
        binding.includeShareholders.tvFundNumMsg1.setText(mShareHolderBean.fundNums[1]);
        binding.includeShareholders.tvFlowThroughSharesMsg0.setText(mShareHolderBean.flows[0]);
        binding.includeShareholders.tvFlowThroughSharesMsg1.setText(mShareHolderBean.flows[1]);
        binding.includeShareholders.tvInstitutionsNumMsg0.setText(mShareHolderBean.institutionsNums[0]);
        binding.includeShareholders.tvInstitutionsNumMsg1.setText(mShareHolderBean.institutionsNums[1]);
        binding.includeShareholders.tvFundNumMsg0.setText(mShareHolderBean.fundNums[0]);
        binding.includeShareholders.tvFundNumMsg1.setText(mShareHolderBean.fundNums[1]);
        binding.includeShareholders.tvActualController.setText(mShareHolderBean.actualController);
        binding.includeShareholders.tvHoldingShareholder.setText(mShareHolderBean.holdingShareholder);

        CommonAdapter<ShareHolderBean.CurbSales> curbSalesCommonAdapter = new CommonAdapter<ShareHolderBean.CurbSales>(this, mShareHolderBean.curbSalesList, R.layout.list_item_stocks_detail_curb_sales) {
            @Override
            public void convert(BaseViewHolder helper, ShareHolderBean.CurbSales item, int position) {
                TextView tvTime = helper.getView(R.id.tv_time);
                TextView tvNum = helper.getView(R.id.tv_num);
                TextView tvProportion = helper.getView(R.id.tv_proportion);
                tvTime.setText(item.date);
                tvNum.setText(item.num);
                tvProportion.setText(item.proportion);
            }
        };
        binding.includeShareholders.lvCurbSales.setAdapter(curbSalesCommonAdapter);

        CommonAdapter<ShareHolderBean.ShareHolderNumber> shareHolderNumberCommonAdapter = new CommonAdapter<ShareHolderBean.ShareHolderNumber>(this, mShareHolderBean.shareHolderNumberList, R.layout.list_item_stocks_detail_curb_sales) {
            @Override
            public void convert(BaseViewHolder helper, ShareHolderBean.ShareHolderNumber item, int position) {
                TextView tvTime = helper.getView(R.id.tv_time);
                TextView tvNum = helper.getView(R.id.tv_num);
                TextView tvProportion = helper.getView(R.id.tv_proportion);
                tvTime.setText(item.date);
                tvNum.setText(item.num);
                tvProportion.setText(item.average);
            }
        };
        binding.includeShareholders.lvShareholdNum.setAdapter(shareHolderNumberCommonAdapter);

        binding.includeShareholders.tablayoutCirculateShareholder.setTabMode(TabLayout.MODE_FIXED);
        for (String title: mShareHolderBean.circulateShareholderdates){
            binding.includeShareholders.tablayoutCirculateShareholder.addTab(binding.includeShareholders.tablayoutCirculateShareholder.newTab().setText(title));
            binding.includeShareholders.tablayoutShareholder.addTab(binding.includeShareholders.tablayoutShareholder.newTab().setText(title));
        }

        CommonAdapter<ShareHolderBean.CirculateShareholderItem> circulateShareholderItemCommonAdapter = new CommonAdapter<ShareHolderBean.CirculateShareholderItem>(this, mShareHolderBean.circulateShareholderItemList, R.layout.list_item_stocks_detail_circulate_shareholder) {
            @Override
            public void convert(BaseViewHolder helper, ShareHolderBean.CirculateShareholderItem item, int position) {
                TextView tvName = helper.getView(R.id.tv_name);
                TextView tvRatio = helper.getView(R.id.tv_ratio);
                TextView tvNum = helper.getView(R.id.tv_num);
                TextView tvChange = helper.getView(R.id.tv_change);
                tvName.setText(item.name);
                tvRatio.setText(item.ratio);
                tvNum.setText(item.num);
                tvChange.setText(item.change);
            }
        };
        binding.includeShareholders.lvCirculateShareholder.setAdapter(circulateShareholderItemCommonAdapter);
        binding.includeShareholders.lvShareholder.setAdapter(circulateShareholderItemCommonAdapter);


    }

    private void initF10Detail() {
        binding.includeF10.tvIndustryContent.setText(mF10Bean.industry);
        binding.includeF10.tvCompanyName.setText(mF10Bean.companyName);
        binding.includeF10.tvEachDistrict.setText(mF10Bean.district);
        binding.includeF10.tvLegalRepresentative.setText(mF10Bean.legalRepresentative);
        binding.includeF10.tvMainBusiness.setText(mF10Bean.mainBusiness);
        binding.includeF10.tvCompanyProfile.setText(mF10Bean.companyProfile);
        binding.includeF10.tvLaunchDate.setText(mF10Bean.launchDate);
        binding.includeF10.tvOfferingPrice.setText(mF10Bean.offerPrice);
        binding.includeF10.tvOfferingNum.setText(mF10Bean.offerNum);
        binding.includeF10.tvIssuePriceRatio.setText(mF10Bean.issuePriceRatio);
        binding.includeF10.tvExpectedFundraising.setText(mF10Bean.expectedFundraising);
        binding.includeF10.tvOnlineSignaturesRate.setText(mF10Bean.onlineSignturesRate);
        binding.includeF10.tvUnderNetworkMatchRate.setText(mF10Bean.underNetworkMatchRate);
        binding.includeF10.tvFundHolding.setText(mF10Bean.fundHolding);
        binding.includeF10.tvForFlowThroughShares.setText(mF10Bean.forFlowThroughShares);
        binding.includeF10.tvIncrementalCapital.setText(mF10Bean.incrementalCapital);
        binding.includeF10.tvIncrementalCapital.setTextColor(Color.RED);
        binding.includeF10.tvRaiseIncreaseDecrease.setText(mF10Bean.raiseIncreaseDecrease);
        binding.includeF10.tvHoldingNum.setText(mF10Bean.holdingNum);
        binding.includeF10.tvHoldFunds.setText(mF10Bean.holdingFunds);
        binding.includeF10.tvHoldStocksIncreaseDecrease.setText(mF10Bean.holdStocksIncreaseDecrease);
        binding.includeF10.tvHoldStocksIncreaseDecrease.setTextColor(Color.RED);
        binding.includeF10.tvFundIncreaseDecrease.setText(mF10Bean.fundIncreaseDecrease);
        binding.includeF10.tvFundIncreaseDecrease.setTextColor(Color.RED);

        CommonAdapter<F10Bean.DistributionPlan> distributionPlanCommonAdapter = new CommonAdapter<F10Bean.DistributionPlan>(this, mF10Bean.distributionPlanList, R.layout.list_item_stocks_detail_distribution_plan) {
            @Override
            public void convert(BaseViewHolder helper, F10Bean.DistributionPlan item, int position) {
                TextView tvTitle = helper.getView(R.id.tv_title);
                TextView tvContent = helper.getView(R.id.tv_content);
                TextView tvDate = helper.getView(R.id.tv_date);
                tvTitle.setText(item.name);
                tvContent.setText(item.content);
                tvDate.setText(item.date);
            }
        };
        binding.includeF10.lvDividendDistribution.setAdapter(distributionPlanCommonAdapter);

        CommonAdapter<F10Bean.EventRemind> eventRemindCommonAdapter = new CommonAdapter<F10Bean.EventRemind>(this, mF10Bean.eventRemindList, R.layout.list_item_stocks_detail_event_remind) {
            @Override
            public void convert(BaseViewHolder helper, F10Bean.EventRemind item, int position) {
                TextView tvTime = helper.getView(R.id.tv_event_time);
                TextView tvContent = helper.getView(R.id.tv_event_content);
                ImageView ivLineTop = helper.getView(R.id.iv_line_top);
                tvTime.setText(item.time);
                tvContent.setText(item.content);
                if (position == 0)
                    ivLineTop.setVisibility(View.INVISIBLE);
                else
                    ivLineTop.setVisibility(View.VISIBLE);


            }
        };
        binding.includeF10.lvEventRemind.setAdapter(eventRemindCommonAdapter);

        CommonAdapter<F10Bean.Concept> conceptCommonAdapter = new CommonAdapter<F10Bean.Concept>(this, mF10Bean.conceptList, R.layout.list_item_stocks_detail_concept) {
            @Override
            public void convert(BaseViewHolder helper, F10Bean.Concept item, int position) {
                TextView tvName = helper.getView(R.id.tv_name);
                TextView tvContent = helper.getView(R.id.tv_content);
                tvName.setText(item.name);
                tvContent.setText(item.content);
            }
        };
        binding.includeF10.gvConcept.setAdapter(conceptCommonAdapter);
    }

    private void initUI() {
        initTabLayout();
        initKLine();
        initListView();

        binding.includeShareholders.lvCurbSales.setFocusable(false);
        binding.includeShareholders.lvShareholdNum.setFocusable(false);
        binding.includeShareholders.lvCirculateShareholder.setFocusable(false);
        binding.includeShareholders.lvShareholder.setFocusable(false);
    }

    private void initKLine() {
        mChartKline = findViewById(R.id.mycombinedchart_k_line);
    }

    @Override
    public void initData() {
        newsList.addAll(SimulatedDataManager.getNewList());
        announcementList.addAll(SimulatedDataManager.getAnnouncementList());
        mF10Bean = SimulatedDataManager.getF10Bean();
        mShareHolderBean = SimulatedDataManager.getShareHolderBean();
        mFinancialBean = SimulatedDataManager.getFinancialBean();
    }


    private void initListView() {
        mNewAdapter = new CommonAdapter<NewBean>(this, newsList, R.layout.list_item_stocks_detail_news) {
            @Override
            public void convert(BaseViewHolder helper, NewBean item, int position) {
                TextView tvTitle = helper.getView(R.id.tv_news_title);
                TextView tvTime = helper.getView(R.id.tv_new_time);
                tvTitle.setText(item.getTitle());
                tvTime.setText(item.getTime());
            }
        };

        mAnnouncementAdapter = new CommonAdapter<NewBean>(this, announcementList, R.layout.list_item_stocks_detail_news) {

            @Override
            public void convert(BaseViewHolder helper, NewBean item, int position) {
                TextView tvTitle = helper.getView(R.id.tv_news_title);
                TextView tvTime = helper.getView(R.id.tv_new_time);
                tvTitle.setText(item.getTitle());
                tvTime.setText(item.getTime());
            }
        };

        binding.lvStocksDetail.setAdapter(mNewAdapter);
        binding.lvStocksDetail.setFocusable(false);
    }

    private void initTabLayout() {
        List<String> list_top_title = Arrays.asList(getResources().getStringArray(R.array.stock_top_detail_tab));
        binding.tablayoutTopStocksDetail.setTabMode(TabLayout.MODE_FIXED);
        for (String title: list_top_title){
            binding.tablayoutTopStocksDetail.addTab(binding.tablayoutTopStocksDetail.newTab().setText(title));
        }

        List<String> list_bottom_title = Arrays.asList(getResources().getStringArray(R.array.stock_bottom_detail_tab));
        binding.tablayoutBottomStocksDetail.setTabMode(TabLayout.MODE_FIXED);
        for (String title: list_bottom_title){
            binding.tablayoutBottomStocksDetail.addTab(binding.tablayoutBottomStocksDetail.newTab().setText(title));
        }


        binding.tablayoutBottomStocksDetail.addOnTabSelectedListener(bottomTabSelectLister);

    }

    private TabLayout.OnTabSelectedListener bottomTabSelectLister = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            switch (tab.getPosition()){
                case 0:
                    binding.lvStocksDetail.setVisibility(View.VISIBLE);
                    binding.lvStocksDetail.setAdapter(mNewAdapter);
                    binding.includeF10.rlStockDetailF10.setVisibility(View.GONE);
                    binding.includeShareholders.llStockDetailShareholders.setVisibility(View.GONE);
                    binding.includeFinancial.llStockDetailFinancial.setVisibility(View.GONE);
                    break;
                case 1:
                    binding.lvStocksDetail.setVisibility(View.VISIBLE);
                    binding.lvStocksDetail.setAdapter(mAnnouncementAdapter);
                    binding.includeF10.rlStockDetailF10.setVisibility(View.GONE);
                    binding.includeShareholders.llStockDetailShareholders.setVisibility(View.GONE);
                    binding.includeFinancial.llStockDetailFinancial.setVisibility(View.GONE);
                    break;
                case 2:
                    binding.lvStocksDetail.setVisibility(View.GONE);
                    binding.includeF10.rlStockDetailF10.setVisibility(View.GONE);
                    binding.includeShareholders.llStockDetailShareholders.setVisibility(View.GONE);
                    binding.includeFinancial.llStockDetailFinancial.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    binding.lvStocksDetail.setVisibility(View.GONE);
                    binding.includeF10.rlStockDetailF10.setVisibility(View.GONE);
                    binding.includeShareholders.llStockDetailShareholders.setVisibility(View.VISIBLE);
                    binding.includeFinancial.llStockDetailFinancial.setVisibility(View.GONE);
                    break;
                case 4:
                    binding.lvStocksDetail.setVisibility(View.GONE);
                    binding.includeF10.rlStockDetailF10.setVisibility(View.VISIBLE);
                    binding.includeShareholders.llStockDetailShareholders.setVisibility(View.GONE);
                    binding.includeFinancial.llStockDetailFinancial.setVisibility(View.GONE);
                    break;
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    };

}
